package com.flowable.service;

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class LeaveFailService implements JavaDelegate {

    @Override
    public void execute(DelegateExecution execution) {
        try {
            String processInstanceId = execution.getProcessInstanceId();
            String employeeName = (String) execution.getVariable("name");
            String leaveReason = (String) execution.getVariable("reason");
            Integer leaveDays = (Integer) execution.getVariable("days");
            String comment = (String) execution.getVariable("comment");

            log.info("开始处理请假失败通知，流程ID: {}, 员工: {}, 原因: {}, 天数: {}",
                    processInstanceId, employeeName, leaveReason, leaveDays);

            // 1. 记录失败日志
            logFailure(execution);


            log.info("请假失败处理完成，流程ID: {}", processInstanceId);
        } catch (Exception e) {
            log.error("处理请假失败通知时出错，流程ID: {}", execution.getProcessInstanceId(), e);
            throw new RuntimeException("处理请假失败通知失败", e);
        }
    }

    private void logFailure(DelegateExecution execution) {
        // 这里可以记录更详细的失败信息到数据库或日志系统
        log.warn("请假申请被拒绝 - 流程ID: {}, 员工: {}, 原因: {}, 审批意见: {}",
                execution.getProcessInstanceId(),
                execution.getVariable("name"),
                execution.getVariable("reason"),
                execution.getVariable("comment"));
    }

}